package com.yc.pre.entity;

import com.yc.pre.consts.StatusCodeConst;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @ClassName Result
 * @Description 统一返回结果类
 * @Date 2021/02/07 16:11:52
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@ApiModel(description = "Result", value = "Result")
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "返回状态码, 20000:成功, 20001:失败, 30001:用户名或密码错误, 30002:请勿重复发送验证码, 30003:验证码错误, 30004:请输入账号，30005:请输入手机号, 30006:账号被锁定, 30008:账号已存在, 30009:手机号不存在, 30010:图片验证码错误, 40001:远程调用失败, 50001:参数错误, 60001：文件上传异常, 70001:权限不足, 70002:请重新登录, 80001:数据未找到, 90001:未知错误", required = true, example = "20000")
    private Integer code;//返回码
    @ApiModelProperty(value = "提示信息", required = true, example = "正确无误")
    private String message;//返回消息
    @ApiModelProperty(value = "逻辑数据", required = true)
    private T data;//返回数据

    public Result(Integer code, String message, Object data){
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message, Object data) {
        // this.flag = flag
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        //this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        //this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功!";
    }

    /*public boolean isFlag() {
        return flag;
    }*/

    /*public void setFlag(boolean flag) {
        this.flag = flag;
    }*/

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    private static <T> Result<T> of(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> isOk(T data) {
        return of(StatusCodeConst.OK.getCode(), StatusCodeConst.OK.getMessage(), data);
    }

    public static <T> Result<T> isOk(T data,String message) {
        return of(StatusCodeConst.OK.getCode(), message, data);
    }

    public static <T> Result<T> isOk() {
        return of(StatusCodeConst.OK.getCode(), StatusCodeConst.OK.getMessage(), null);
    }

    public static <T> Result<T> isFail(StatusCodeConst resp) {
        return of(resp.getCode(), resp.getMessage(), null);
    }

    public static <T> Result<T> isFail(StatusCodeConst resp, String message) {
        return of(resp.getCode(), message, null);
    }

    public static <T> Result<T> isFail(String message) {
        return of(StatusCodeConst.DEFAULT_ERROR.getCode(), message, null);
    }

    public static <T> Result<T> isFail(String message, T data) {
        return of(StatusCodeConst.DEFAULT_ERROR.getCode(), message, data);
    }
}
